package com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.controller;

import com.anjiplus.template.gaea.business.modules.quartz.controller.QuartzTaskController;
import com.anjiplus.template.gaea.business.modules.quartz.mode.QuartzForm;
import com.anjiplus.template.gaea.business.modules.quartz.util.Result;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.dao.CompanyTaskMapper;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.entity.CompanyTask;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.form.TBScheduledGaeaReportForm;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.service.TBScheduledGaeaReportService;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.vo.TBScheduledGaeaReportVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * TBScheduledGaeaReport前端控制器
 *
 * </p>
 *
 * @author admin
 * @since 2023-08-24
 */
@RestController
@Api(tags = "TBScheduledGaeaReport")
@RequestMapping("/tBScheduledGaeaReport")
public class TBScheduledGaeaReportController {
    @Autowired
    private TBScheduledGaeaReportService tBScheduledGaeaReportService;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private QuartzTaskController quartzTaskController;
    @Autowired
    private CompanyTaskMapper companyTaskMapper;

    /**
     * 保存单条
     *
     * @param param 保存参数
     * @return 是否添加成功
     */
    @ApiOperation(value = "保存", notes = "保存数据到TBScheduledGaeaReport")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = false),
            @ApiImplicitParam(paramType = "query", name = "revision", value = "乐观锁", required = false),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false),
            @ApiImplicitParam(paramType = "query", name = "updated_by", value = "更新人", required = false),
            @ApiImplicitParam(paramType = "query", name = "updatedTime", value = "更新时间", required = false),
            @ApiImplicitParam(paramType = "query", name = "delete_state", value = "删除状态", required = false),
            @ApiImplicitParam(paramType = "query", name = "gaea_report_code", value = "报表编码", required = false),
            @ApiImplicitParam(paramType = "query", name = "scheduled_code", value = "定时任务表达式", required = false),
    })
    @PostMapping(value = "/add.do")
    public Result addTBScheduledGaeaReport(@RequestBody TBScheduledGaeaReportForm param) {
        if (1 != tBScheduledGaeaReportService.save(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }

    /**
     * 更新(根据主键id更新)
     *
     * @param param 修改参数
     * @return 是否更改成功
     */
    @ApiOperation(value = "更新数据", notes = "根据主键id更新TBScheduledGaeaReport数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "revision", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updated_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updatedTime", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "delete_state", value = "删除状态", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gaea_report_code", value = "报表编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "scheduled_code", value = "定时任务表达式", required = false, dataType = ""),
    })
    @PostMapping(value = "/updateById.do")
    public Result updateTBScheduledGaeaReportById(@RequestBody TBScheduledGaeaReportForm param) {
        if (1 != tBScheduledGaeaReportService.updateById(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }


    /**
     * 删除(根据主键id删除)
     *
     * @param //主键id
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除数据", notes = "根据主键id删除TBScheduledGaeaReport数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @PostMapping(value = "/deleteById.do")
    @Transactional
    public Result deleteTBScheduledGaeaReportById(@RequestBody TBScheduledGaeaReportForm param) {
        int result = 0;
        String id = param.getId().toString();
        TBScheduledGaeaReportVO tbScheduledGaeaReportVO = tBScheduledGaeaReportService.selectById(id);
        LambdaQueryWrapper<CompanyTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyTask::getTaskGroup, tbScheduledGaeaReportVO.getScheduledCode());
        CompanyTask companyTask = companyTaskMapper.selectOne(wrapper);
        // companyTask不为空再删除
        if (null != companyTask) {
            QuartzForm quartzForm = new QuartzForm();
            quartzForm.setCompanyTaskId(companyTask.getId());
            quartzTaskController.deleteTask(quartzForm);
        }
        tBScheduledGaeaReportService.deleteById(id);
        if (result < 0) {
            return Result.fail("删除失败！");
        }
        return Result.success("操作成功");
    }

    /**
     * 根据主键id查询单条
     *
     * @param // 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "获取单条数据", notes = "根据主键id获取TBScheduledGaeaReport数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @PostMapping(value = "/getById.do")
    public Result getTBScheduledGaeaReportById(@RequestParam(value = "Id") String id) {
        TBScheduledGaeaReportVO result = tBScheduledGaeaReportService.selectById(id);

        return Result.success("操作成功", result);
    }

    /**
     * 查询全部
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "全部查询", notes = "查询TBScheduledGaeaReport全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "revision", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updated_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updatedTime", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "delete_state", value = "删除状态", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gaea_report_code", value = "报表编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "scheduled_code", value = "定时任务表达式", required = false, dataType = ""),
    })
    @PostMapping(value = "/queryAll.do")
    public Result getTBScheduledGaeaReportAll(@RequestBody TBScheduledGaeaReportForm param) {
        List<TBScheduledGaeaReportVO> result = tBScheduledGaeaReportService.selectAll(param);

        return Result.success("操作成功", result);
    }

    /**
     * 分页查询
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询", notes = "分页查询TBScheduledGaeaReport全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "revision", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updated_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updatedTime", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "delete_state", value = "删除状态", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gaea_report_code", value = "报表编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "scheduled_code", value = "定时任务表达式", required = false, dataType = ""),
    })
    @PostMapping(value = "/queryPage.do")
    public Result getTBScheduledGaeaReportPage(@RequestBody TBScheduledGaeaReportForm param) {
        IPage<TBScheduledGaeaReportVO> result = tBScheduledGaeaReportService.selectPage(param);

        return Result.convertIpageToSuccess(result);
    }


}

